package com.updesignsz.admin.mapper;

import com.updesignsz.commom.entity.Article;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 文章
 * Creation date 2020-8-8 15:06:04
 */
@Repository
@Mapper
public interface ArticleMapper {

    @Select({"<script>",
            "select * from article ",
            "<where>",
            "<if test=\"type != null\" >AND type = #{type}</if>",
            "AND is_removed = 0",
            "</where>",
            "order by created_date desc",
            "</script>"
    })
    List<Article> listAll(Article type);


    @Select("select * from article where id = #{id}")
    Article getById(@Param("id") Integer id);

    @Insert({
            "insert into article(title, `type`, cover, cover_thumbnail, content, is_removed, created_date, updated_date) ",
            "values (#{title},#{type},#{cover},#{coverThumbnail},#{content},0,NOW(),NOW())"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    void saveArticle(Article type);


    @Update({"<script>",
            "update article <set>",
            "<if test=\"title != null\" >title = #{title},</if>",
            "<if test=\"type != null\" >type = #{type},</if>",
            "<if test=\"cover != null\" >cover = #{cover},</if>",
            "<if test=\"coverThumbnail != null\" >cover_thumbnail = #{coverThumbnail},</if>",
            "<if test=\"content != null\" >content = #{content},</if>",
            "updated_date = NOW()",
            "</set> where id = #{id}",
            "</script>"
    })
    void updateArticle(Article type);

    @Delete("delete from article where id = #{id}")
    void deleteArticle(@Param("id") Integer id);
}